<?php


namespace app\controller\partner;


use app\BaseController;
use app\common\exception\ApiException;
use app\model\partner\EscortMenuReserveOrderModel;
use app\model\partner\PartnerBusyTimeModel;
use app\service\config\ConfigService;

class DutyController extends BaseController
{
    /**
     * 值班列表
     * @return \think\response\Json
     */
    public function getDutyList()
    {
        try {
            $userId = $this->request->middleware('userId');
            $dayString = $this->request->param('day', date('Y-m-d'), 'trim');

            $day = strtotime($dayString);

            $workTimeArr = ConfigService::getConfigValueArray(['work_time_start', 'work_time_end']);

            $startTime = $workTimeArr['work_time_start'];
            $endTime = $workTimeArr['work_time_end'];

            $timeArr = PartnerBusyTimeModel::where('partner_id', $userId)
                ->where('day', $day)
                ->column('time');

            $list = [];
            $j = 0;
            for ($i = $startTime; $i <= $endTime; $i++) {
                if (in_array($i, $timeArr)) {
                    $isBusy = 1;
                } else {
                    $isBusy = 0;
                }
                $list[$j] = [
                    'time' => $i,
                    'type' => $isBusy
                ];
                $j++;
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }


    public function getDayList()
    {
        try {
            $userId = $this->request->middleware('userId');
            $day = $this->request->param('day', '', 'trim');
            $list = PartnerBusyTimeModel::where('partner_id', $userId)
                ->where('day', $day)
                ->field('id,start_time,end_time,type')
                ->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 设置忙碌时间
     * @return \think\response\Json
     */
    public function setBusyTime()
    {
        try {
            $userId = $this->request->middleware('userId');
            $dayString = $this->request->param('day', date('Y-m-d'), 'trim');
            if (!$dayString) {
                throw new ApiException('请选择日期');
            }
            $time = $this->request->param('time', 0, 'int');

            if ($time) {
                $partnerBusyTimeModel = new PartnerBusyTimeModel();
                $partnerBusyTimeModel->partner_id = $userId;
                $partnerBusyTimeModel->day = strtotime($dayString);
                $partnerBusyTimeModel->time = $time;
                $partnerBusyTimeModel->save();
            } else {
                $workTimeArr = ConfigService::getConfigValueArray(['work_time_start', 'work_time_end']);

                $startTime = $workTimeArr['work_time_start'];
                $endTime = $workTimeArr['work_time_end'];

                $dataAll = [];
                $j = 0;
                for ($i = $startTime; $i <= $endTime; $i++) {
                    $dataAll[$j] = ['time' => $i, 'partner_id' => $userId, 'day' => strtotime($dayString)];
                    $j++;
                }
                $partnerBusyTimeModel = new PartnerBusyTimeModel();
                $partnerBusyTimeModel->saveAll($dataAll);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 取消
     * @return \think\response\Json
     */
    public function cancel()
    {
        try {
            $userId = $this->request->middleware('userId');
            $dayString = $this->request->param('day', date('Y-m-d'), 'trim');
            if (!$dayString) {
                throw new ApiException('请选择日期');
            }
            $time = $this->request->param('time', 0, 'int');

            $info = PartnerBusyTimeModel::where('partner_id', $userId)->where('day', strtotime($dayString))->where('time', $time)->find();
            if ($info) {
                $info->delete();
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}